package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.service.IContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 合同Service业务层处理
 * 
 * @author ZHD
 * @date 2025-08-11
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询合同
     * 
     * @param id 合同主键
     * @return 合同
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectById(id);
    }

    /**
     * 查询合同列表
     * 
     * @param contract 合同
     * @return 合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        return contractMapper.insert(contract);
    }

    /**
     * 修改合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        return contractMapper.updateById(contract);
    }

    /**
     * 批量删除合同
     * 
     * @param ids 需要删除的合同主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return contractMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除合同信息
     * 
     * @param id 合同主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return contractMapper.deleteById(id);
    }

    /**
     * 更新合同状态
     * 0--->1：未生效--->1:已生效
     * 1--->2：已生效--->2:已过期
     * 0--->3：未生效--->3:已失效
     */
    @Override
    public void updateContractStatus() {
        LocalDateTime now = LocalDateTime.now();
        //0--->1：未生效--->1:已生效
        List<Contract> list = this.lambdaQuery()
                .eq(Contract::getStatus, 0)
                .le(Contract::getStartDate, now)
                .ge(Contract::getEndDate, now)
                .list();
        if (CollUtil.isNotEmpty(list)) {
            list.forEach(item -> {
                item.setStatus(1);
                this.updateById(item);
            });
        }
        //1--->2：已生效--->2:已过期
        list=this.lambdaQuery().eq(Contract::getStatus, 1)
                .le(Contract::getEndDate, now)
                .list();
        if (CollUtil.isNotEmpty(list)){
            list.forEach(item -> {
                item.setStatus(2);
                this.updateById(item);
            });
        }
        //0--->3：未生效--->3:已失效
        list=this.lambdaQuery().eq(Contract::getStatus, 0)
                .ge(Contract::getEndDate, now)
                .list();
        if (CollUtil.isNotEmpty(list)){
            list.forEach(item -> {
                item.setStatus(3);
                this.updateById(item);
            });
        }
    }
}
